<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="100px">
      <el-form-item label="设备名称" prop="deviceId">
        <el-select
          v-model="queryParams.deviceId"
          placeholder="请选择设备"
          clearable
          filterable
        >
          <el-option v-for="device in deviceList" :key="device.deviceId" :label="device.deviceName" :value="device.deviceId"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="维修状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择维修状态" clearable>
          <el-option label="待维修" value="0"></el-option>
          <el-option label="维修中" value="1"></el-option>
          <el-option label="已完成" value="2"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="维修完成时间" prop="repairTime">
        <el-date-picker clearable
          v-model="queryParams.repairTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择维修完成时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="维修人员" prop="technician">
        <el-input
          v-model="queryParams.technician"
          placeholder="请输入维修人员"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['monitor:record:add']"
        >新增维修记录</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['monitor:record:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['monitor:record:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['monitor:record:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="recordList" @selection-change="handleSelectionChange" border>
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="记录编号" align="center" prop="recordId" width="100" />
      <el-table-column label="设备名称" align="center" prop="deviceName" min-width="120" :show-overflow-tooltip="true" />
      <el-table-column label="故障描述" align="center" prop="faultDescription" min-width="180" :show-overflow-tooltip="true" />
      <el-table-column label="维修状态" align="center" prop="status" width="100">
        <template slot-scope="scope">
          <el-tag type="warning" v-if="scope.row.status == 0">待维修</el-tag>
          <el-tag type="primary" v-else-if="scope.row.status == 1">维修中</el-tag>
          <el-tag type="success" v-else-if="scope.row.status == 2">已完成</el-tag>
          <el-tag v-else>未知</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="维修完成时间" align="center" prop="repairTime" width="120">
        <template slot-scope="scope">
          <span v-if="scope.row.repairTime">{{ parseTime(scope.row.repairTime, '{y}-{m}-{d}') }}</span>
          <span v-else>-</span>
        </template>
      </el-table-column>
      <el-table-column label="维修人员" align="center" prop="technician" width="120" />
      <el-table-column label="创建时间" align="center" prop="createTime" width="120">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="180">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['monitor:record:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['monitor:record:remove']"
          >删除</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="handleView(scope.row)"
          >查看</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改设备维修记录对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="设备" prop="deviceId">
          <el-select
            v-model="form.deviceId"
            placeholder="请选择设备"
            filterable
          >
            <el-option v-for="device in deviceList" :key="device.deviceId" :label="device.deviceName" :value="device.deviceId"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="故障描述" prop="faultDescription">
          <el-input v-model="form.faultDescription" type="textarea" :rows="4" placeholder="请输入故障描述" />
        </el-form-item>
        <el-form-item label="维修状态" prop="status">
          <el-select v-model="form.status" placeholder="请选择维修状态">
            <el-option label="待维修" value="0"></el-option>
            <el-option label="维修中" value="1"></el-option>
            <el-option label="已完成" value="2"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="维修人员" prop="technician">
          <el-input v-model="form.technician" placeholder="请输入维修人员" />
        </el-form-item>
        <el-form-item label="维修完成时间" prop="repairTime" v-if="form.status == 2">
          <el-date-picker clearable
            v-model="form.repairTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择维修完成时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="维修备注" prop="repairNotes">
          <el-input v-model="form.repairNotes" type="textarea" :rows="3" placeholder="请输入维修备注信息" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 查看维修记录详情对话框 -->
    <el-dialog title="维修记录详情" :visible.sync="viewOpen" width="600px" append-to-body>
      <el-descriptions :column="2" border>
        <el-descriptions-item label="记录编号">{{ viewForm.recordId }}</el-descriptions-item>
        <el-descriptions-item label="设备名称">{{ viewForm.deviceName }}</el-descriptions-item>
        <el-descriptions-item label="维修状态">
          <el-tag type="warning" v-if="viewForm.status == 0">待维修</el-tag>
          <el-tag type="primary" v-if="viewForm.status == 1">维修中</el-tag>
          <el-tag type="success" v-if="viewForm.status == 2">已完成</el-tag>
        </el-descriptions-item>
        <el-descriptions-item label="维修人员">{{ viewForm.technician }}</el-descriptions-item>
        <el-descriptions-item label="维修完成时间" v-if="viewForm.repairTime">
          {{ parseTime(viewForm.repairTime, '{y}-{m}-{d}') }}
        </el-descriptions-item>
        <el-descriptions-item label="故障报告时间">
          {{ parseTime(viewForm.createTime, '{y}-{m}-{d} {h}:{i}:{s}') }}
        </el-descriptions-item>
        <el-descriptions-item label="故障描述" :span="2">{{ viewForm.faultDescription }}</el-descriptions-item>
        <el-descriptions-item label="维修备注" :span="2" v-if="viewForm.repairNotes">{{ viewForm.repairNotes }}</el-descriptions-item>
      </el-descriptions>
      <div slot="footer" class="dialog-footer">
        <el-button @click="viewOpen = false">关闭</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listRecord, getRecord, delRecord, addRecord, updateRecord } from "@/api/newMonitor/repair";
import { listDevice } from "@/api/newMonitor/device";

export default {
  name: "Record",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 设备维修记录表格数据
      recordList: [],
      // 设备列表
      deviceList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 是否显示详情弹出层
      viewOpen: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        deviceId: null,
        repairTime: null,
        technician: null,
        status: null,
      },
      // 表单参数
      form: {},
      // 详情表单
      viewForm: {},
      // 表单校验
      rules: {
        deviceId: [
          { required: true, message: "设备不能为空", trigger: "change" }
        ],
        faultDescription: [
          { required: true, message: "故障描述不能为空", trigger: "blur" }
        ],
        status: [
          { required: true, message: "维修状态不能为空", trigger: "change" }
        ],
        technician: [
          { required: true, message: "维修人员不能为空", trigger: "blur" }
        ],
        repairTime: [
          { required: true, message: "维修完成时间不能为空", trigger: "change" }
        ]
      }
    };
  },
  created() {
    this.getList();
    this.getDeviceList();
  },
  methods: {
    /** 查询设备维修记录列表 */
    getList() {
      this.loading = true;
      listRecord(this.queryParams).then(response => {
        // 处理数据，将ID转换为名称
        const rows = response.rows || [];
        rows.forEach(row => {
          // 设置设备名称
          const device = this.deviceList.find(d => d.deviceId == row.deviceId);
          row.deviceName = device ? device.deviceName : row.deviceId;
        });
        
        this.recordList = rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 获取设备列表
    getDeviceList() {
      listDevice().then(response => {
        this.deviceList = response.rows || [];
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        recordId: null,
        deviceId: null,
        faultDescription: null,
        repairTime: null,
        technician: null,
        status: "0", // 默认为待维修状态
        repairNotes: null,
        createTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.recordId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加设备维修记录";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const recordId = row.recordId || this.ids
      getRecord(recordId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改设备维修记录";
      });
    },
    /** 查看详情 */
    handleView(row) {
      // 复制数据并设置设备名称
      this.viewForm = { ...row };
      this.viewOpen = true;
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          // 如果状态不是"已完成"，清空维修完成时间
          if (this.form.status !== "2") {
            this.form.repairTime = null;
          }
          
          if (this.form.recordId != null) {
            updateRecord(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addRecord(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const recordIds = row.recordId || this.ids;
      this.$modal.confirm('是否确认删除设备维修记录编号为"' + recordIds + '"的数据项？').then(function() {
        return delRecord(recordIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('monitor/record/export', {
        ...this.queryParams
      }, `record_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>

<style scoped>
.el-tag {
  margin-right: 5px;
}
</style>
